#ifndef __EssBuffer_H__
#define __EssBuffer_H__

class EssBuffer
{
public:
	EssBuffer() : mBuffer(0), mSize(0) {}

	virtual ~EssBuffer()
	{
		if (mBuffer)
			free(mBuffer);
	}

	inline void* base() const { return mBuffer; }
	inline int size() const { return mSize; }

	inline void clear()
	{
		if (mBuffer)
		{
			free(mBuffer);
			mBuffer = 0;
			mSize = 0;
		}
	}

	inline void push(const void* p, int len)
	{
		int size = mSize + len;
		mBuffer = realloc(mBuffer, size);
		assert(mBuffer != NULL);
		memcpy((void*)((char*)mBuffer + mSize), p, len);
		mSize = size;
	}

	inline void pop(int len)
	{
		len = min(len, mSize);
		if (len == 0)
			return;
		else if (len == mSize)
			clear();
		else
		{
			int size = mSize - len;
			memcpy(mBuffer, (const void*)((const char*)mBuffer + len), size);
			mSize = size;
		}
	}

protected:
	void*	mBuffer;
	int		mSize;
};

#endif //__EssBuffer_H__